package com.eco.fanliapp.job.orderjobNew;

import com.eco.fanliapp.common.OrderTaskUtils;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IJobOrderInfoHandleNewService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * 拉取2小时付款订单
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class OrderTwoHourTaskNew implements Job, Serializable {

    private static final Logger log = LoggerFactory.getLogger(OrderTwoHourTaskNew.class);
    private String timeFlag ="";

    @Autowired
    private IJobOrderInfoHandleNewService jobOrderInfoHandleNewService;

    @Autowired
    private OrderTaskUtils orderTaskUtils;

    public OrderTwoHourTaskNew(){

    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //取得job详情
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }
        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long beginTime = System.currentTimeMillis();
        log.info("***************************************开始执行定时任务-拉取2小时订单定时任务-OrderTwoHourTaskNew：executeCount="+executeCount+"--beginTime="+beginTime);

        //获取淘宝联盟账号-排除已过期的
        List<TaobaoScheme> taobaoSchemes = orderTaskUtils.getUsedTaobaoSchemeList();
        //切分时间
        String startTime = OrderTaskUtils.getTwoHourSystemTime();

        String endTime = OrderTaskUtils.getSystemTime();

        log.info("开始拉取2小时订单-开始时间{}、结束时间{}", startTime, endTime);
        //处理订单
        jobOrderInfoHandleNewService.handlePayOrder(startTime, endTime, taobaoSchemes, 1);
        jobOrderInfoHandleNewService.handleSettleOrder(startTime, endTime, taobaoSchemes, 1);

        log.info("***************************************结束执行定时任务-拉取2小时订单定时任务-OrderTwoHourTaskNew：executeCount=" + executeCount + "--endTime=" + System.currentTimeMillis() + "--耗时=" + (System.currentTimeMillis() - beginTime));

    }
//
//    /**
//     * 处理时间
//     * @param timeFlag
//     * @return
//     */
//    public List<String> handleTime(String timeFlag){
//        //当前日期参照点
//        Date currentTime = new Date();
//        String currentTimeStr = DateUtils.date2Str(currentTime, null);
//        if (timeFlag == null || "".equals(timeFlag)) {
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(currentTime);
//            calendar.add(Calendar.MINUTE, -(2 * 60 + 20));
//
//            timeFlag = DateUtils.date2Str(calendar.getTime(), null);
//            log.info("拉取2小时订单定时任务-开始2小时时间参照点{}", timeFlag);
//        }
//        //切割时间段(交易类型 M/D/H/N -->每月/每天/每小时/每分钟,多久分成一份，开始时间，结束时间)
//        List<String> list = DateCalculateUtils.cutDate("N",1200, timeFlag, currentTimeStr);
//        return list;
//    }

    public static void main(String[] args) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
////        calendar.add(Calendar.MINUTE, -(24*60+20));
//
//        String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(calendar.getTime());
//        handleTime("");
//        handleTime1(timeFlag);
        OrderTwoHourTaskNew ts = new OrderTwoHourTaskNew();
//        ts.handleTime("");
    }


}
